package com.itcast.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itcast.common.res.ResultCode;
import com.itcast.mapper.UserMapper;
import com.itcast.pojo.User;
import com.itcast.service.Userservice;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.bind.ValidationException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-06-19
 */
@Service("userservice")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Userservice {

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     * @param user
     * @return
     */
    @Override
    public boolean addUser(User user) {
        boolean falg = this.save(user);
        return falg;
    }

    /**
     * 登录
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        User us = userMapper.login(user);
        return us;
    }

    @Override
    public int updatePwd(String pwd, String phone) {
        return userMapper.updatePwd(pwd,phone);
    }
    @Override
    public int updateAvatar(Integer userId,String imgUrl) throws ValidationException {
        User user=new User();
        user.setUid(userId);
        user.setAvatar(imgUrl);
        int uid = userMapper.update(user, new QueryWrapper<User>().eq("uid", userId));
        if (uid<1){
            throw new ValidationException(ResultCode.UPDATE_FAIL);
        }
        return uid;
    }

    @Override
    public User userLogin(String phone, String password) {
        return userMapper.select(phone, password);
    }

    @Override
    public int register(String phone, String password) {
        return userMapper.register(phone,password);
    }

    @Override
    public int updateUserInfo(User user) {
        return userMapper.updateUserInfo(user);
    }

    @Override
    public int updateUserPassword(String uid, String password) {
        return userMapper.updateUserPassword(uid,password);
    }


}

